package com.adapters;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.objects.OutboxObject;
import com.senditcertified.mail.R;

public class OutboxAdapter extends BaseExpandableListAdapter {
	private Context myContext;
	private List<List<OutboxObject>> myData;

	public OutboxAdapter(Context context,
			List<List<OutboxObject>> data) {
		myContext = context;
		myData = data;
		
//		Log.i("NGOC", "=============");
//		for (OutboxObject tmp : data.keySet()) {
//			Log.i("NGOC", tmp.getDateTime() + " | " + tmp.getDescription());
//		}
	}

	@Override
	public OutboxObject getChild(int groupPosition, int childPosition) {
		OutboxObject item = null;
		try {
			item = getGroup(groupPosition).get(childPosition);
		} catch (Exception ex) {
		}
		return item;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = LayoutInflater.from(this.myContext).inflate(
					R.layout.outbox_item_child, parent, false);
			
		}
		TextView tvMail = (TextView) convertView.findViewById(R.id.tv_mail);
		ImageView imvStatus = (ImageView) convertView
				.findViewById(R.id.img_pending);
		View viewTop = convertView.findViewById(R.id.ll_top);
		
		OutboxObject object = getChild(groupPosition, childPosition);
		if (childPosition == 0) {
			viewTop.setVisibility(View.VISIBLE);
		} else {
			viewTop.setVisibility(View.GONE);
		}
		
		if(object.getStatus().trim().equals("Accepted")){
			imvStatus.setImageResource(R.drawable.out_box_ic_accepted);
		}else if(object.getStatus().trim().equals("Failed")){
			imvStatus.setImageResource(R.drawable.out_box_ic_failed);
		}else if(object.getStatus().trim().equals("Recalled")){
			imvStatus.setImageResource(R.drawable.out_box_ic_recalled);
		}else{
			imvStatus.setImageResource(R.drawable.out_box_ic_pending);
		}
		
		tvMail.setText(object.getRecippient());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return getGroup(groupPosition).size();
	}

	@Override
	public List<OutboxObject> getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return myData.get(groupPosition);
	}

	public OutboxObject getGroupName(int groupPosition) {
		return myData.get(groupPosition).get(0);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return myData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		OutboxObject headerTitle = getGroupName(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) this.myContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.out_box_item_group, parent,
					false);
			
		}
		TextView tvDate = (TextView) convertView.findViewById(R.id.tv_date);
		TextView tvHours = (TextView) convertView.findViewById(R.id.tv_hours);
		TextView tvContent = (TextView) convertView
				.findViewById(R.id.tv_content);
		ImageView imgAdd = (ImageView) convertView.findViewById(R.id.img_add);
		ImageView attachment = (ImageView) convertView.findViewById(R.id.img_upload);
		
		tvContent.setText(headerTitle.getDescription());
		
		int spaceIndex = headerTitle.getDateTime().indexOf(" ");
		String date = headerTitle.getDateTime().substring(0, spaceIndex);
		String hour = headerTitle.getDateTime().substring(spaceIndex+1);
		
		tvDate.setText(date);
		tvHours.setText(hour);
		if (isExpanded) {
			imgAdd.setImageDrawable(myContext.getResources()
					.getDrawable(R.drawable.outbox_ic_sub_item));
		} else {
			imgAdd.setImageDrawable(myContext.getResources()
					.getDrawable(R.drawable.outbox_ic_add));
		}
		
		if(headerTitle.getAttach().trim().length() > 0){
			attachment.setVisibility(View.VISIBLE);
		}else{
			attachment.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
}
